//
//  GQHTrucksView.m
//  Xanthium
//
//  Created by GuanQinghao on 08/03/2018.
//  Copyright © 2018 GuanQinghao. All rights reserved.
//

#import "GQHTrucksView.h"
#import "GQHCustomCell.h"
#import "GQHHeader.h"

@interface GQHTrucksView () <UITableViewDelegate,UITableViewDataSource,GQHTrucksCellDelegate>

@end


@implementation GQHTrucksView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        
        [self loadLocalData];
        [self loadUserInterface];
    }
    return self;
}

#pragma mark --Data
- (void)loadLocalData {
    
}

#pragma mark --View
- (void)loadUserInterface {
    
    UIView * topView = [[UIView alloc] init];
    topView.backgroundColor = kLightGrayColor;
    [self addSubview:topView];
    [topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self).with.inset(self.qh_statusBarHeight + self.qh_navigationBarHeight);
        make.left.and.right.mas_equalTo(self);
        make.height.mas_equalTo(55);
    }];
    
    UITextField * textField = [[UITextField alloc] init];
    textField.placeholder = @"搜索关键字";
    textField.borderStyle = UITextBorderStyleRoundedRect;
    textField.clearButtonMode = UITextFieldViewModeWhileEditing;
    [topView addSubview:textField];
    self.textField = textField;
    [textField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(topView).with.inset(5);
        make.left.mas_equalTo(topView).with.inset(5);
        make.right.mas_equalTo(topView).with.inset(80);
        make.height.mas_equalTo(45);
    }];
    
    UIButton * searchButton = [[UIButton alloc] init];
    searchButton.backgroundColor = kAppRedColor;
    searchButton.titleLabel.font = [UIFont systemFontOfSize:15];
    [searchButton setTitleColor:kWhiteColor forState:0];
    [searchButton setTitle:@"搜索" forState:0];
    [searchButton addTarget:self action:@selector(searchKeyWordsAction:) forControlEvents:UIControlEventTouchUpInside];
    searchButton.layer.cornerRadius = 5;
    searchButton.layer.masksToBounds = YES;
    [topView addSubview:searchButton];
    [searchButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(topView).with.inset(5);
        make.left.mas_equalTo(textField.mas_right).with.inset(10);
        make.right.mas_equalTo(topView.mas_right).with.inset(10);
        make.height.mas_equalTo(45);
    }];
    
    UIView *filterView = [[UIView alloc] init];
    filterView.backgroundColor = kLightGrayColor;
    [self addSubview:filterView];
    [filterView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(topView.mas_bottom).with.inset(5);
        make.left.and.right.mas_equalTo(self);
        make.height.mas_equalTo(55);
    }];
    
    UIButton *departureButton = [UIButton buttonWithType:UIButtonTypeCustom];
    departureButton.backgroundColor = kWhiteColor;
    departureButton.titleLabel.font = kDefaultFont;
    [departureButton setTitle:@"起始地" forState:UIControlStateNormal];
    [departureButton setTitleColor:kDarkBlackColor forState:UIControlStateNormal];
    [departureButton addTarget:self action:@selector(checkDeparture:) forControlEvents:UIControlEventTouchUpInside];
    [filterView addSubview:departureButton];
    [departureButton mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.and.left.mas_equalTo(filterView);
        make.width.mas_equalTo(filterView).with.multipliedBy(0.25);
        make.bottom.mas_equalTo(filterView).with.inset(5);
    }];
    
    UIButton *destinationButton = [UIButton buttonWithType:UIButtonTypeCustom];
    destinationButton.backgroundColor = kWhiteColor;
    destinationButton.titleLabel.font = kDefaultFont;
    [destinationButton setTitle:@"目的地" forState:UIControlStateNormal];
    [destinationButton setTitleColor:kDarkBlackColor forState:UIControlStateNormal];
    [destinationButton addTarget:self action:@selector(checkDestination:) forControlEvents:UIControlEventTouchUpInside];
    [filterView addSubview:destinationButton];
    [destinationButton mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.and.bottom.mas_equalTo(departureButton);
        make.left.mas_equalTo(departureButton.mas_right);
        make.width.mas_equalTo(departureButton);
    }];
    
    UIButton *typeButton = [UIButton buttonWithType:UIButtonTypeCustom];
    typeButton.backgroundColor = kWhiteColor;
    typeButton.titleLabel.font = kDefaultFont;
    [typeButton setTitle:@"全部" forState:UIControlStateNormal];
    [typeButton setTitleColor:kDarkBlackColor forState:UIControlStateNormal];
    [typeButton addTarget:self action:@selector(checkType:) forControlEvents:UIControlEventTouchUpInside];
    [filterView addSubview:typeButton];
    [typeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.and.bottom.mas_equalTo(destinationButton);
        make.left.mas_equalTo(destinationButton.mas_right);
        make.width.mas_equalTo(destinationButton);
    }];
    
    UIButton *moreButton = [UIButton buttonWithType:UIButtonTypeCustom];
    moreButton.backgroundColor = kWhiteColor;
    moreButton.titleLabel.font = kDefaultFont;
    [moreButton setTitle:@"更多" forState:UIControlStateNormal];
    [moreButton addTarget:self action:@selector(checkMore:) forControlEvents:UIControlEventTouchUpInside];
    [moreButton setTitleColor:kDarkBlackColor forState:UIControlStateNormal];
    [filterView addSubview:moreButton];
    [moreButton mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.and.bottom.mas_equalTo(typeButton);
        make.left.mas_equalTo(typeButton.mas_right);
        make.width.mas_equalTo(typeButton);
    }];
    
    for (NSInteger i = 0; i < 3; i++) {
        
        CGFloat centerX = self.qh_width * 0.25 * (i + 1);
        
        UIView *splitorView = [[UIView alloc] init];
        splitorView.backgroundColor = kLightGrayColor;
        [filterView addSubview:splitorView];
        [splitorView mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.top.and.bottom.mas_equalTo(filterView).with.inset(10);
            make.left.mas_equalTo(centerX);
            make.width.mas_equalTo(2);
        }];
        [filterView bringSubviewToFront:splitorView];
    }
    
    _qh_tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    _qh_tableView.delegate = self;
    _qh_tableView.dataSource = self;
    _qh_tableView.backgroundColor = kWhiteColor;
    _qh_tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _qh_tableView.showsVerticalScrollIndicator = NO;
    if (@available(iOS 11.0, *)) {
        
        _qh_tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }
    [self addSubview:_qh_tableView];
    [_qh_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(filterView.mas_bottom);
        make.left.and.right.mas_equalTo(self);
        make.bottom.mas_equalTo(self.mas_bottom).with.inset(self.qh_tabBarHeight + self.qh_homeIndicatorHeight);
    }];
}

#pragma mark --Delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return _qh_truckModelArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 80.0f;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *identifier = @"cell";
    GQHTrucksCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        
        cell = [[GQHTrucksCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    
    cell.qh_delegate = self;
    cell.qh_dialButton.tag = indexPath.row;
    
    GQHTruckModel *truckModel = _qh_truckModelArray[indexPath.row];
    
    /// 头像
    NSString *URLString = [[NSString qh_imageURLString] stringByAppendingPathComponent:truckModel.qh_driverAvatar];
    NSURL *URL = [NSURL URLWithString:URLString];
    [cell.qh_avatarImageView sd_setImageWithURL:URL placeholderImage:[UIImage qh_imageWithColor:kLightGrayColor]];
    
    /// 姓名
    cell.qh_nameLabel.text = truckModel.qh_driverName;
    /// 车辆信息
    cell.qh_contentLabel.text = [NSString stringWithFormat:@"%@/%@/%@吨",truckModel.qh_normalLength,truckModel.qh_model,truckModel.qh_normalLoading];
    /// 路线
    cell.qh_routeLabel.text = [NSString stringWithFormat:@"%@-%@",truckModel.qh_departure,truckModel.qh_destination];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if ([_qh_delegate respondsToSelector:@selector(qh_selectedRowWithTableView:atIndexPath:)]) {
        
        [_qh_delegate qh_selectedRowWithTableView:tableView atIndexPath:indexPath];
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    return CGFLOAT_MIN;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    return [UIView new];
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    
    return CGFLOAT_MIN;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    
    return [UIView new];
}

- (void)searchKeyWordsAction:(UIButton *)sender
{
    if (self.textField.text.length == 0)
    {
        return;
    }
    
    if ([_qh_delegate respondsToSelector:@selector(searchKeyWord:)]) {
        
        [_qh_delegate searchKeyWord:self.textField.text];
    }
}

- (IBAction)checkDeparture:(UIButton *)sender {
    DLog(@"点击选择起始地");
    
    if ([_qh_delegate respondsToSelector:@selector(qh_checkDeparture:)]) {
        
        [_qh_delegate qh_checkDeparture:sender];
    }
}

- (IBAction)checkDestination:(UIButton *)sender {
    DLog(@"点击选择目的地");
    
    if ([_qh_delegate respondsToSelector:@selector(qh_checkDestination:)]) {
        
        [_qh_delegate qh_checkDestination:sender];
    }
}

- (IBAction)checkType:(UIButton *)sender {
    DLog(@"点击选择来源类型");
    
    if ([_qh_delegate respondsToSelector:@selector(qh_checkType:)]) {
        
        [_qh_delegate qh_checkType:sender];
    }
}

- (IBAction)checkMore:(UIButton *)sender {
    DLog(@"点击选择更多");
    
    if ([_qh_delegate respondsToSelector:@selector(qh_checkMore:)]) {
        
        [_qh_delegate qh_checkMore:sender];
    }
}

- (void)qh_dialMobile:(UIButton *)sender {
    
    if ([_qh_delegate respondsToSelector:@selector(qh_dialMobile:)]) {
        
        [_qh_delegate qh_dialMobile:sender];
    }
}

@end
